//http://192.168.2.123:8080/video
#include "v4a_hal.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include "bmp_encoder.h"
#include "USER_CFG.h"
#include "filecopy.h"
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "fileupdate.h"
#include "spi.h"

#include <libxml/xmlmemory.h> 
#include <libxml/parser.h> 

#define IOCTL_LCD_BRIGHTNESS	_IOW('v', 27, unsigned int)	//brightness control
#define VIDEO_DISPLAY_ON	_IOW('v', 24, unsigned int)	//display on
#define VIDEO_DISPLAY_OFF	_IOW('v', 25, unsigned int)
#define FB_DEV "/dev/fb0"
#define FIFO	"/fifo"

int fifo_fd=0;

extern int date_config_flag;
extern int time_config_en;
extern int date_config_en;
extern int real_offset;
extern int time_offset;
extern int date_offset;
extern int date_change;
extern int time_change;
extern int cur_bit;
extern int Dt_last_weekday;

char userbkgnd_bmp[4*1366*768] __attribute__ ((aligned(4)));
char userbkgnd_bmp_data[1366*768*4] __attribute__ ((aligned(4)));

extern IMGNODE bkgnd_fbcopy;
extern IMGNODE bkgnd_fbdisplay;
extern IMGNODE overlay_fbcopy;
extern IMGNODE overlay_fbdisplay;
extern IMGNODE flex_fbdisplay;
extern unsigned int pic_addr;

extern unsigned int GUI_LCM_XMAX,GUI_LCM_YMAX,LCD_CLK;
extern REGION G_REG_DIR;
extern REGION G_REG_FLR;
extern REGION G_REG_TIME;
extern REGION G_REG_DAY;
extern REGION G_REG_WEEK;
extern REGION G_REG_ALARM;
extern REGION G_REG_BKGND;
extern REGION G_REG_LOGO;
extern REGION G_REG_OTHER;
extern REGION G_REG_VIDEO;
extern REGION G_REG_TXT;
unsigned int BKGND_wait;
unsigned int BKGND_effect;

extern unsigned int G_LFTTXT_txt_vfmode;
extern unsigned int G_LFTTXT_vf_speed;
extern unsigned int G_LFTTXT_txt_color;

REGION G_REG_DIR_T2;
REGION G_REG_FLR_T2;
REGION G_REG_ALARM_T2;

REGION REG_DES;
extern filestruct File_RAM[FOLDER_NUM][FILE_NUM_IN_FOLDER];

volatile unsigned int LIFT_FLAG;
volatile unsigned int Flag_CN;
int Native_Volume=0;
int Time_Bkgnd=0;
int Light_Off_Time=0;
int Sleep_Brightness=0;

_enum_DMODE G_DispMode = dmode_0C;

unsigned int display_tmp_addr=8*1024*1024;

extern IMGNODE ptr;
REGION DisplayRegion[11];
Flr_Map flr_map[255];

flr flr_n;
char dir_num;
char state_num;
char lift_info_bak[6];

LIFT_WORK_INFO lift_work_info;

char * pDir_fixbkg[4] =
{
	"/v4asys/LIFT/LIFT_0C/BKGND/FLR_BSD/fixedbkgnd.bmp",
	"/v4asys/LIFT/LIFT_90C/BKGND/FLR_BSD/fixedbkgnd.bmp",
	"/v4asys/LIFT/LIFT_180C/BKGND/FLR_BSD/fixedbkgnd.bmp",
	"/v4asys/LIFT/LIFT_270C/BKGND/FLR_BSD/fixedbkgnd.bmp",
};

#define MAXLINE 80
#define SERV_PORT 8000

#define DEBUG 1

char weather_info0[4][4];

//图片信息结构体
typedef struct media_play_info{
	
	unsigned char media_play_type[20];//videofile or txtfile
	unsigned char media_play_filename[40];//1.jpg,2.jpg...
	time_t media_play_startdate;
	time_t media_play_stopdate;
	time_t media_play_starttime;
	time_t media_play_stoptime;
	int media_play_staytime;//图片停留时间
	int media_play_times;//播放次数
	
}MEDIAPLAYINFO; 

MEDIAPLAYINFO meida_info0[1];
MEDIAPLAYINFO meida_info[512];
MEDIAPLAYINFO meida_info_cur[1];
MEDIAPLAYINFO meida_info_next[1];

int media_num = 0;

struct tm info_time1;
struct tm *info_time=&info_time1;
int year,mon,day;
int h,min,sec;
int jpg_staytime;
int jpg_times;

char program_list[256][40];
int program_list_num = 0;

int Emergency_Flag=0;
int Emergency_Flag_Last=0;

int Notice_Flag = 1;
int Weather_Flag = 1;
int Logo_Flag =1;

int AF_FLAG = 0;


void  send_cmd() //通过有名管道向mplayer发送命令   
{  
	char volum[20];
	memset(volum,0,20);
	FILE *VOLUME = fopen("/mnt/bootdev/volume.cfg","rb");	
	int vol=fgetc(VOLUME);	
	fclose(VOLUME);
	sprintf(volum,"volume %d 1\n",vol);
	
	//sprintf(volum,"volume %d 1\n",15);
	
	if ((write(fifo_fd,volum,strlen(volum)))!=strlen(volum))  
	{
		perror("write cmd");  
	}  
}  

void  send_pause() //通过有名管道向mplayer发送命令   
{  
	char volum[20];
	memset(volum,0,20);
	strcpy(volum,"pause\n");
	
	
	if ((write(fifo_fd,volum,strlen(volum)))!=strlen(volum))  
	{
		perror("write cmd");  
	}  
}  

void  send_mute(int enable) //通过有名管道向mplayer发送命令   
{  
	char volum[20];
	memset(volum,0,20);
	sprintf(volum,"mute %d\n",enable);
		
	if ((write(fifo_fd,volum,strlen(volum)))!=strlen(volum))  
	{
		perror("write cmd");  
	}  
}  

int myplay(char* cmd)
{   
    char volum[20];
    pid_t child, status;
    if ((child = fork()) < 0) 
    {
        perror("fork");
        return 1;
    }
    
    if(child == 0){
        execlp("./mplayer" , "mplayer" , "-slave" , "-quiet" , "-input" , "file=/fifo" , cmd, NULL);
        return -1;
    } 
    else {
		sleep(2);
		send_mute(1);
		if(wait(&status) < 0) {
			perror("wait");
			exit(1);
		}
    }
    return status;
}

getReference(xmlNodePtr cur)
{
	char* value=NULL;
	cur=cur->xmlChildrenNode;

	while(cur !=NULL)
	{

		if( !xmlStrcmp("Video",(char*)(cur->name)) )
		{		
			media_num++;
			memset(meida_info[media_num-1].media_play_type,0,20);
			value=(char*)xmlGetProp(cur,"type");
			if(value!=NULL)
			{			
			sprintf(meida_info[media_num-1].media_play_type,"%s",value);
			xmlFree(value);	
			if(DEBUG)
			printf("\ntype is %s\n",meida_info[media_num-1].media_play_type);	
			}
		}

		if( (!xmlStrcmp("FileName",(char*)(cur->name))) )
		{
			memset(meida_info[media_num-1].media_play_filename,0,40);
			
			value=(char*)xmlNodeGetContent(cur);	
			if(value!=NULL)
			{	
			sprintf(meida_info[media_num-1].media_play_filename,"%s",value);
			xmlFree(value);			
			if(DEBUG)
			printf("FileName is: %s\n",meida_info[media_num-1].media_play_filename);
			}
		}

		if( (!xmlStrcmp("StartDate",(char*)(cur->name))) )
		{

			value=(char*)xmlNodeGetContent(cur);
			if(value!=NULL)
			{			
			sscanf(value,"%d-%d-%d",&year,&mon,&day);
			info_time->tm_year=(year-1900);
			info_time->tm_mon=mon-1;
			info_time->tm_mday=day;
			info_time->tm_hour=0;
			info_time->tm_min=0;
			info_time->tm_sec=0;
			xmlFree(value);
			meida_info[media_num-1].media_play_startdate=mktime(info_time);
			if(DEBUG)
			printf("StartDate is: %d\n",(int)meida_info[media_num-1].media_play_startdate);
			}
		
		}

		
		if( (!xmlStrcmp("StopDate",(char*)(cur->name))) )
		{
			value=(char*)xmlNodeGetContent(cur);			
			if(value!=NULL)
			{
			sscanf(value,"%d-%d-%d",&year,&mon,&day);
			info_time->tm_year=(year-1900);
			info_time->tm_mon=mon-1;
			info_time->tm_mday=day;
			info_time->tm_hour=23;
			info_time->tm_min=59;
			info_time->tm_sec=59;
			xmlFree(value);
			meida_info[media_num-1].media_play_stopdate=mktime(info_time);
			if(DEBUG)
			printf("StopDate is: %d\n",(int)meida_info[media_num-1].media_play_stopdate);
			}
		}

		if( (!xmlStrcmp("StartTime",(char*)(cur->name))) )
		{
			value=(char*)xmlNodeGetContent(cur);			
			if(value!=NULL)
			{
			sscanf(value,"%d:%d",&h,&min);
			meida_info[media_num-1].media_play_starttime=h*3600+min*60;
			xmlFree(value);
			if(DEBUG)
			printf("StartTime is: %d\n",(int)meida_info[media_num-1].media_play_starttime);
			}
		}

		if( (!xmlStrcmp("StopTime",(char*)(cur->name))) )
		{
			value=(char*)xmlNodeGetContent(cur);			
			if(value!=NULL)
			{
			sscanf(value,"%d:%d",&h,&min);
			meida_info[media_num-1].media_play_stoptime=h*3600+min*60+59;
			xmlFree(value);
			if(DEBUG)
			printf("StopTime is: %d\n",(int)meida_info[media_num-1].media_play_stoptime);
			}
		}

		if( (!xmlStrcmp("StayTime",(char*)(cur->name))) )
		{
			value=(char*)xmlNodeGetContent(cur);			
			if(value!=NULL)
			{
			sscanf(value,"%d",&jpg_staytime);
			meida_info[media_num-1].media_play_staytime=jpg_staytime;
			xmlFree(value);
			if(DEBUG)
			printf("StayTime is: %d s\n",meida_info[media_num-1].media_play_staytime);
			}
		}
		
		
		if( (!xmlStrcmp("Region",(char*)(cur->name))) )
		{
			value=(char*)xmlNodeGetContent(cur);			
			if(value!=NULL)
			{
			sscanf(value,"%d",&jpg_times);
			meida_info[media_num-1].media_play_times=jpg_times;
			xmlFree(value);
			if(DEBUG)
			printf("Play Times is: %d\n",meida_info[media_num-1].media_play_times);
			}
		}
		
		if( (!xmlStrcmp("MD5",(char*)(cur->name))) )
		{
			value=(char*)xmlNodeGetContent(cur);
			if(value!=NULL)
			{
			if(DEBUG)
			printf("MD5 is: %s\n",value);			
			xmlFree(value);
			}
		}
		getReference(cur);
		cur=cur->next; 
	}
}

int get_media_list()
{
	char addr[100];
	xmlDocPtr doc=NULL;
	xmlNodePtr cur=NULL;
	
	memset(addr,0,100);
	strcpy(addr, "/v4asys/VIDEO/native.xml");	

	xmlKeepBlanksDefault (0);//忽略空白行 
	
	doc=xmlParseFile(addr);//创建Dom树
	if(doc==NULL)
	{
		printf("Loading xml file failed.\n");
		return 1; 
	}
	cur=xmlDocGetRootElement(doc);//获取根节点
	if(cur==NULL)
	{
		printf("empty file\n");
		xmlFreeDoc(doc); 
		return 2; 
	}

	media_num=0;
	getReference(cur);
	xmlFreeDoc(doc);	

	return 0;
}


int get_program_list()
{
	xmlDocPtr doc=NULL;
	xmlNodePtr    cur=NULL;
	char* name=NULL;
	char* value=NULL;
	xmlKeepBlanksDefault (0);//忽略空白行 
	
	int i;
	
	for(i=0;i<256;i++)
	{
		memset(program_list[i],0,40);
	}
	
	i = 0;
	
	doc=xmlParseFile("/v4asys/VIDEO/order.xml");//创建Dom树
	if(doc==NULL)
	{
	   printf("Loading xml file failed.\n");
	   program_list_num = 0;
	   return 0; 
	}
	cur=xmlDocGetRootElement(doc);//获取根节点
	if(cur==NULL)
	{
	   printf("empty file\n");
	   xmlFreeDoc(doc); 
	   program_list_num = 0;
	   return 0; 
	}
	
	//walk the tree 
	cur=cur->xmlChildrenNode;//get sub node
	while(cur !=NULL)
	{
	   name=(char*)(cur->name); 
	   value=(char*)xmlNodeGetContent(cur);
	   if(value!=NULL)
		{
	   printf("program_list[%d] is: %s\n",i,value);
	   
	   strcpy(program_list[i],value);
	   i++;
	   
	   xmlFree(value); 
		}
	   cur=cur->next; 
	}
	xmlFreeDoc(doc);//释放xml解析库所用资源
	xmlCleanupParser();
	
	program_list_num = i;
	
	return 0; 
}


int get_weather_info()
{
	xmlDocPtr doc=NULL;
	xmlNodePtr    cur=NULL;
	char* name=NULL;
	char* value=NULL;
	xmlKeepBlanksDefault (0);//忽略空白行 
	
	int i;
	
	for(i=0;i<4;i++)
	{
		memset(weather_info0[i],0,4);
	}
	
	i = 0;
	
	
	doc=xmlParseFile("/v4asys/VIDEO/weather.xml");//创建Dom树
	if(doc==NULL)
	{
	   printf("Loading xml file failed.\n");
	   return 0; 
	}
	cur=xmlDocGetRootElement(doc);//获取根节点
	if(cur==NULL)
	{
	   printf("empty file\n");
	   xmlFreeDoc(doc); 
	   return 0; 
	}
	
	//walk the tree 
	cur=cur->xmlChildrenNode;//get sub node
	while(cur !=NULL)
	{
	   name=(char*)(cur->name); 
	   value=(char*)xmlNodeGetContent(cur);
	   if(value!=NULL)
		{
	   printf("weather_info0[%d] is: %s\n",i,value);
	   
	   strcpy(weather_info0[i],value);
	   i++;
	   
	   xmlFree(value); 
		}
	   cur=cur->next; 
	}
	xmlFreeDoc(doc);//释放xml解析库所用资源
	xmlCleanupParser();
	
	printf("program_list_num = %d", program_list_num);
	
	return 0; 
}

int media_is_valid(char *list)
{
	int i;
	time_t cur_time;
	int cur_time_t;
	char picture_full_name[64];
	
	int st=0;
	if(list == NULL)
		return 0;
	
	for(i=0;i<media_num;i++)
	{
		if( (strstr(list, meida_info[i].media_play_filename)!=NULL) && (strstr( meida_info[i].media_play_filename,list)!=NULL) )
		{
			time(&cur_time);
			info_time = localtime(&cur_time);
			cur_time_t = info_time->tm_hour*3600 + info_time->tm_min*60 + info_time->tm_sec;
			
			if( (meida_info[i].media_play_startdate<=cur_time) && (meida_info[i].media_play_stopdate>=cur_time) )
			if( (meida_info[i].media_play_starttime<=cur_time_t) && (meida_info[i].media_play_stoptime>=cur_time_t) )
			{
				memset(picture_full_name,0,64);
				strcpy(picture_full_name,"/v4asys/area1/");		
				strcat(picture_full_name,list);
				FILE *pictu = fopen(picture_full_name,"r");
	
				if(pictu!=NULL)
				{
					fclose(pictu);
					st = meida_info[i].media_play_staytime+1;
					printf("\nst[%d] = %d\n",i,st);
					return st;
				}
				
			}			
		}
	}
	return 0;
}

int txt_is_valid(char *list)
{
	int i;
	time_t cur_time;
	int cur_time_t;
	char picture_full_name[64];
	
	if(list == NULL)
		return 0;
	
	for(i=0;i<media_num;i++)
	{
		if( (strstr(list, meida_info[i].media_play_filename)!=NULL) && (strstr(list, meida_info[i].media_play_filename)!=NULL) )
		{
			time(&cur_time);
			info_time = localtime(&cur_time);
			cur_time_t = info_time->tm_hour*3600 + info_time->tm_min*60 + info_time->tm_sec;
			
			if( (meida_info[i].media_play_startdate<=cur_time) && (meida_info[i].media_play_stopdate>=cur_time) )
			{
			if( (meida_info[i].media_play_starttime<=cur_time_t) && (meida_info[i].media_play_stoptime>=cur_time_t) )
			{
				memset(picture_full_name,0,64);
				strcpy(picture_full_name,"/v4asys/VIDEO/");		
				strcat(picture_full_name,list);
				FILE *pictu = fopen(picture_full_name,"r");
	
				if(pictu!=NULL)
				{
					fclose(pictu);
					return 1;
				}
				else
				printf("not exist\n");	
				
			}
			else
			printf("not time\n");	
			}
			else
			printf("not date\n");			
		}
		
	}
	//printf("not in xml\n");
	return 0;
}

int usbdisk_is_insert()
{
	FILE *p=NULL;
	char bInterfaceClassNum[10];
	
	if( access("/sys/devices/platform/w55fa95-ohci/usb1/1-1/1-1:1.0/bInterfaceClass", 0) == 0)
	{
		p = fopen("/sys/devices/platform/w55fa95-ohci/usb1/1-1/1-1:1.0/bInterfaceClass","r");
		if(p==NULL)
		{
			printf("open bInterfaceClass failed\n");
			return 0;
		}

		memset(bInterfaceClassNum,0,10);
		fread(bInterfaceClassNum,10,1,p);
		fclose(p);
		p=NULL;
		if( (bInterfaceClassNum[0]=='0') && (bInterfaceClassNum[1]=='8') )
		{
			//printf("bInterfaceClass = %s\n",bInterfaceClassNum);
			return 1;
		}
	}
	
	
	if( access("/sys/devices/platform/w55fa95-ohci/usb1/1-2/1-2:1.0/bInterfaceClass", 0) == 0)
	{
		p = fopen("/sys/devices/platform/w55fa95-ohci/usb1/1-2/1-2:1.0/bInterfaceClass","r");
		if(p==NULL)
		{
			printf("open bInterfaceClass failed\n");
			return 0;
		}

		memset(bInterfaceClassNum,0,10);
		fread(bInterfaceClassNum,10,1,p);
		fclose(p);
		p = NULL;
		if( (bInterfaceClassNum[0]=='0') && (bInterfaceClassNum[1]=='8') )
		{
			//printf("bInterfaceClass = %s\n",bInterfaceClassNum);
			return 1;
		}
	}
	return 0;						

}

//安抚音乐播放线程
void *Appease_Music_pthread()
{
	char Music_File_Path[40];
	int Music_Play_Times = 3;
	int i;
	
	while(1)
	{
		if(Emergency_Flag)
		{
			memset(Music_File_Path, 0, 40);
			strcpy(Music_File_Path, "/v4asys/area0/0.mp3");
			
			if(access(Music_File_Path, 0) == 0)
			{
				for(i=0;i<3;i++)
				{
					if(AF_FLAG == 1)
					{
						AF_FLAG = 0;
						break;
					}
					myplay(Music_File_Path);
				}
			}
			Emergency_Flag = 0;
		}
		else
		{
			AF_FLAG = 0;
			memset(Music_File_Path, 0, 40);
			strcpy(Music_File_Path, "/v4asys/area0/1.mp3");
			
			if(access(Music_File_Path, 0) == 0)
			{
					myplay(Music_File_Path);
			}
			
		}
		sleep(1);
	}
	
	return 0;
}


void *get_lift_pthread0()
{
	char buffer1[12];
	struct sockaddr_in servaddr, cliaddr;
	socklen_t cliaddr_len;
	int sockfd, connfd_temp, tcp_socket_readlen;
	char buf[100];
	char buf_opt[100];
	struct timeval outtime;
	memset(buf,0,100);
	outtime.tv_sec = 3;
	outtime.tv_usec = 1000;
	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	setsockopt(sockfd, SOL_SOCKET,SO_RCVTIMEO, &outtime,sizeof(outtime));

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(5555);
	bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
	
	memset(buf_opt,0,100);
	buf_opt[0]='\0';
	int size = 0;
	
	while(1)
	{
		tcp_socket_readlen=0;
		cliaddr_len = sizeof(cliaddr);
		memset(buf,0,100);
		tcp_socket_readlen=recvfrom(sockfd, buf, 100, 0, (struct sockaddr *)&cliaddr, &cliaddr_len);
		if(tcp_socket_readlen>0)
		{
			memcpy(&buf_opt[size],buf,tcp_socket_readlen);
			int length = tcp_socket_readlen+size;
			char *bfp = buf_opt;
			memset(buffer1,0,12);
			printf("\n1\n");
			for(connfd_temp = 0;connfd_temp<length;connfd_temp++)
				printf("%02x",buf_opt[connfd_temp]);
			printf("\n");
			while(length >= 12)
			{	
			if(*bfp==0x02)
			{				
				memcpy(buffer1,bfp,12);
				bfp += 12;
				length -= 12;
				
				printf("\n2\n");
				for(connfd_temp = 0;connfd_temp<12;connfd_temp++)
					printf("%02x",buffer1[connfd_temp]);
				printf("\n");
				
				if(buffer1[11]==0x03)
				{
					if(buffer1[10]==0x41)
					{
						get_media_list();//xml
						get_program_list();//xml
						Notice_Flag = 1;
					}
					else if(buffer1[10]==0x42)
					{
						get_weather_info();
						Weather_Flag = 1;
					}
					else if(buffer1[10]==0x43)
					{
						send_cmd();
					}
					else if(buffer1[10]==0x44)
					{
						Logo_Flag = 1;
					}
					else if(buffer1[10]==0x45)
					{
						if(Emergency_Flag==1)
							Emergency_Flag = 1;
						else
							Emergency_Flag = 2;
					}					
					else
					{
						dir_num = buffer1[1];
						flr_n.flr1 = buffer1[2];
						flr_n.flr2 = buffer1[3];
						flr_n.flr3 = buffer1[4];
						state_num = buffer1[5];	
						if(state_num == '8')
						{
							Emergency_Flag = 1;
						}
						else
						{
							if(Emergency_Flag == 2)
								Emergency_Flag = 2;
							else
								Emergency_Flag = 0;
						}
					}				
				}
				else
				{
					dir_num = ' ';
					flr_n.flr1 = ' ';
					flr_n.flr2 = ' ';
					flr_n.flr3 = ' ';
					state_num = ' ';
				}
						
			}
			else
			{
				bfp++;
				length--;
				continue;
				dir_num = ' ';
				flr_n.flr1 = ' ';
				flr_n.flr2 = ' ';
				flr_n.flr3 = ' ';
				state_num = ' ';
			}
			memset(buffer1,0,12);
			}
			
			if(length > 0)
			{
				memcpy(buffer1,bfp,length);
				memset(buf_opt,0,100);		
				memcpy(buf_opt,buffer1,length);
				size = length;
			}
			else
			{
				memset(buf_opt,0,100);
			}
			
		}

		usleep(100000);
	}

}

void *get_lift_pthread1()
{
	struct sockaddr_in servaddr, cliaddr;
	socklen_t cliaddr_len;
	int sockfd, connfd_temp, tcp_socket_readlen,num;
	char buf[100];
	struct timeval outtime;
	memset(buf,0,100);
	outtime.tv_sec = 0;
	outtime.tv_usec = 1000;
	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	setsockopt(sockfd, SOL_SOCKET,SO_RCVTIMEO, &outtime,sizeof(outtime));

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	servaddr.sin_port = htons(8100);
	bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
	listen(sockfd, 2);

	while(1)
	{
	for(num=0;num<4;num++)
	{
	tcp_socket_readlen=0;
	cliaddr_len = sizeof(cliaddr);
	tcp_socket_readlen=recvfrom(sockfd, buf, 80, 0, (struct sockaddr *)&cliaddr, &cliaddr_len);
	if(tcp_socket_readlen>0)
	{
		switch(buf[0])
		{
			case 'c':
				date_config_flag=1-date_config_flag;
				if(date_config_flag==1)
				{
					printf("config mod start\n");
				}
				else if(date_config_flag==0)
				{
					real_offset=0;
					date_offset=0;
					printf("config mod exit\n");
				}
        		break;

			case 'a':				
				date_change=1;					
				printf("add 1\n");
        		break;

			case 's':				
				date_change=-1;
				printf("sub 1\n");
        		break;
 
			case 'o':
				if(date_config_flag==1)
				{
					real_offset=real_offset+1;
					if(real_offset<=3)
						date_offset = real_offset;
					else if((real_offset>=4)&&(real_offset<=5))
						date_offset=real_offset+1;
					else if((real_offset>=6)&&(real_offset<=7))
						date_offset=real_offset+2;
					else if((real_offset>=8)&&(real_offset<=9))
						date_offset=real_offset+3;
					else if((real_offset>=10)&&(real_offset<=16))
						date_offset=real_offset+4;
					else if(real_offset>=17)
					{
						real_offset=0;
						date_offset=0;
					}
					printf("date_offset=%d\n",date_offset);
				}
        		break;
 
			default: 
        			printf("default\n");
        		break;
		}

	}
			
		usleep(100000);
	}
		cur_bit=1-cur_bit;
	}

}


int Date_Time_Config(void)
{
	
	unsigned int temp;	
	char buf[100];
	char station_data[100];
	int readlen,index;
	FILE *p;
	int i;
	int dpx=0;
	int dpy=324;
	int width_sum=0;
	int width_max=0;
	int height_max=0;
	IMGNODE userbkgnd_pic;
	BMPINFO* info;
	
	int spaceoffset;
	
	time_t timer =time(NULL);
	struct tm *sys_time = localtime(&timer);
	int character_code;
	memset(station_data,0,100);

	//年
	station_data[0] = '2';
	station_data[1] = '0';
	temp = sys_time->tm_year % 100;
	station_data[2] = (temp / 10)+'0';
	temp = sys_time->tm_year % 10;
	station_data[3] = temp+'0';

	station_data[4] = '.';
	
	//月
	temp = (sys_time->tm_mon+1) / 10;
	station_data[5] = temp+'0';
	temp = (sys_time->tm_mon+1) % 10;
	station_data[6] = temp+'0';

	station_data[7] = '.';

	//日
	temp = sys_time->tm_mday / 10;
	station_data[8] = temp+'0';
	temp = sys_time->tm_mday % 10;
	station_data[9] = temp+'0';
	
	station_data[10] = '-';

	//时
	temp = sys_time->tm_hour / 10;
	station_data[11] = '0' + temp;
	temp = sys_time->tm_hour % 10;
	station_data[12] = '0' + temp;
	
	station_data[13] = ':';
	
	//分
	temp = sys_time->tm_min / 10;
	station_data[14] = '0' + temp;
	temp = sys_time->tm_min % 10;
	station_data[15] = '0' + temp;
	
	//station_data[16] = ':';
	
	station_data[16] = '\0';
	
	//秒
	//temp = sys_time->tm_sec / 10;
	//station_data[17] = '0' + temp;
	//temp = sys_time->tm_sec % 10;
	//station_data[18] = '0' + temp;
	
	//station_data[19] = '\0';
	
	if(date_config_flag==1)
	if(date_offset<16)
	{
		
		station_data[date_offset]=station_data[date_offset]+date_change;
		if( station_data[date_offset] > '9')
			station_data[date_offset]='0';
		else if( station_data[date_offset] < '0')
			station_data[date_offset]='9';
		
		if((station_data[2]-'0')*10+station_data[3]-'0'>=36)
		{
			station_data[2]='3';
			station_data[3]='0';
		}

		if((station_data[5]-'0')*10+station_data[6]-'0'>=13)
		{
			station_data[5]='1';
			station_data[6]='0';
		}

		if((station_data[8]-'0')*10+station_data[9]-'0'>=32)
		{
			station_data[8]='3';
			station_data[9]='0';
		}

		if((station_data[5]-'0')*10+station_data[6]-'0'==0)
		{
			station_data[5]='0';
			station_data[6]='1';
		}

		if((station_data[8]-'0')*10+station_data[9]-'0'==0)
		{
			station_data[8]='0';
			station_data[9]='1';
		}
		
		if((station_data[11]-'0')*10+station_data[12]-'0'>=24)
		{
			station_data[11]='2';
			station_data[12]='0';
		}

		if((station_data[14]-'0')*10+station_data[15]-'0'>=60)
		{
			station_data[14]='5';
			station_data[15]='0';
		}

		if(date_change!=0)
		{
		printf("date_offset=%d\n",date_offset);
		memset(buf,0,100);
		sprintf(buf,"date 20%c%c.%c%c.%c%c-%c%c:%c%c",station_data[2],station_data[3],station_data[5],station_data[6],station_data[8],station_data[9],station_data[11],station_data[12],station_data[14],station_data[15]);
		system(buf);
		system("hwclock -w");
		date_change=0;
		}
		
		if(cur_bit)
			station_data[date_offset]=' ';
	}
	Display_Date_Time(station_data);
}

int Volume_Config(void)
{
	FILE * VOLUME;
	char buffer[20];
	if( (date_offset==16) && (date_change!=0) )
	{
		Native_Volume += date_change;		
		if(Native_Volume<0)
			Native_Volume=0;
		if(Native_Volume>99)
			Native_Volume=99;
			
		VOLUME = fopen("/mnt/bootdev/volume.cfg","wb");
		fputc(Native_Volume,VOLUME);
		fclose(VOLUME);
		system("sync");
		date_change=0;
	}
	memset(buffer,0,20);
	sprintf(buffer,"VOL=[%d]",Native_Volume);
	
	if( (cur_bit==1) && (date_offset==16) )
	buffer[strlen(buffer)-2]=' ';
	
	Display_Date_Time(buffer);
}

int Language_Config(void)
{
	FILE * VOLUME;
	if( (date_offset==17) && (date_change!=0) )
	{
		Flag_CN = 1-Flag_CN;					
		VOLUME = fopen("/mnt/bootdev/language.txt","wb");
		fputc(Flag_CN,VOLUME);
		fclose(VOLUME);
		system("sync");
		date_change=0;
	}
	
	if( (cur_bit==1) && (date_offset==17) )
		Display_Date_Time("LANGUAGE=  ");
	else
	{
		if(Flag_CN)
			Display_Date_Time("LANGUAGE=CN");
		else
			Display_Date_Time("LANGUAGE=EN");
		
	}
	
}

int Bkgnd_Time_Config(void)
{
	FILE * VOLUME;
	char buffer[20];
	if( (date_offset==18) && (date_change!=0) )
	{
		Time_Bkgnd += date_change;		
		if(Time_Bkgnd<0)
			Time_Bkgnd=0;
		if(Time_Bkgnd>99)
			Time_Bkgnd=99;					
		VOLUME = fopen("/mnt/bootdev/bkgndtime.txt","wb");
		fputc(Time_Bkgnd,VOLUME);
		fclose(VOLUME);
		system("sync");
		date_change = 0;
	}
	
	memset(buffer,0,20);
	sprintf(buffer,"BKGND_TIME=[%2d]",Time_Bkgnd);
	if( (cur_bit==1) && (date_offset==18) )
	buffer[strlen(buffer)-2]=' ';
	
	Display_Date_Time(buffer);
	
}

int Light_Off_Time_Config(void)
{
	FILE * VOLUME;
	char buffer[20];
	if( (date_offset==19) && (date_change!=0) )
	{
		Light_Off_Time+= date_change;		
		if(Light_Off_Time<0)
			Light_Off_Time=0;
		if(Light_Off_Time>99)
			Light_Off_Time=99;					
		VOLUME = fopen("/mnt/bootdev/light.txt","wb");
		fputc(Light_Off_Time,VOLUME);
		fclose(VOLUME);
		system("sync");
		date_change = 0;
	}
	
	memset(buffer,0,20);
	sprintf(buffer,"SLEEP_WAIT_TIME=[%2d]",Light_Off_Time);
	if( (cur_bit==1) && (date_offset==19) )
		buffer[strlen(buffer)-2]=' ';
	
	Display_Date_Time(buffer);
	
}

int Sleep_Brightness_Config(void)
{
	FILE * VOLUME;
	char buffer[20];
	if( (date_offset==20) && (date_change!=0) )
	{
		Sleep_Brightness += date_change;		
		if(Sleep_Brightness<0)
			Sleep_Brightness=0;
		if(Sleep_Brightness>99)
			Sleep_Brightness=99;					
		VOLUME = fopen("/mnt/bootdev/bl.txt","wb");
		fputc(Sleep_Brightness,VOLUME);
		fclose(VOLUME);
		system("sync");
		date_change = 0;
	}
	
	memset(buffer,0,20);
	sprintf(buffer,"SLEEP_BRIGHTNESS=[%2d]",Sleep_Brightness);
	if( (cur_bit==1) && (date_offset==20) )
		buffer[strlen(buffer)-2]=' ';
	
	Display_Date_Time(buffer);
	
}

int Config_Init(void)
{
	FILE *VOLUME;
	VOLUME = fopen("/mnt/bootdev/volume.cfg","rb");		
	if(VOLUME == NULL)
	{
		VOLUME = fopen("/mnt/bootdev/volume.cfg","wb");
		fputc(15,VOLUME);
		fclose(VOLUME);
		system("sync");
		
		Native_Volume=15;
	}
	else
	{
		Native_Volume=fgetc(VOLUME);	
		fclose(VOLUME);
	}
	
	VOLUME = fopen("/mnt/bootdev/language.txt","rb");		
	if(VOLUME == NULL)
	{
		VOLUME = fopen("/mnt/bootdev/language.txt","wb");
		fputc(1,VOLUME);
		fclose(VOLUME);
		system("sync");
		
		Flag_CN=1;
	}
	else
	{
		Flag_CN=fgetc(VOLUME);	
		fclose(VOLUME);
	}
	
	VOLUME = fopen("/mnt/bootdev/bkgndtime.txt","rb");		
	if(VOLUME == NULL)
	{
		VOLUME = fopen("/mnt/bootdev/bkgndtime.txt","wb");
		fputc(15,VOLUME);
		fclose(VOLUME);
		system("sync");
		
		Time_Bkgnd=15;
	}
	else
	{
		Time_Bkgnd=fgetc(VOLUME);	
		fclose(VOLUME);
	}
	
	VOLUME = fopen("/mnt/bootdev/light.txt","rb");		
	if(VOLUME == NULL)
	{
		VOLUME = fopen("/mnt/bootdev/light.txt","wb");
		fputc(15,VOLUME);
		fclose(VOLUME);
		system("sync");
		
		Light_Off_Time=15;
	}
	else
	{
		Light_Off_Time=fgetc(VOLUME);	
		fclose(VOLUME);
	}
	
	VOLUME = fopen("/mnt/bootdev/bl.txt","rb");		
	if(VOLUME == NULL)
	{
		VOLUME = fopen("/mnt/bootdev/bl.txt","wb");
		fputc(30,VOLUME);
		fclose(VOLUME);
		system("sync");
		
		Sleep_Brightness=30;
	}
	else
	{
		Sleep_Brightness=fgetc(VOLUME);	
		fclose(VOLUME);
	}
	
}

int enable_backlight()
{
	int fb_fd;
	int brightness = 99;
	
	if ((fb_fd = open(FB_DEV, O_RDWR)) < 0) {
		printf("open device %s failed", FB_DEV);
		return -1;
	}
	ioctl(fb_fd, VIDEO_DISPLAY_ON);
	ioctl(fb_fd, IOCTL_LCD_BRIGHTNESS, &brightness);
	//sleep(1);
	close(fb_fd);
	return 0;
}

int Display_Fixedbkgnd()
{
	int i=0;
	FILE *p = fopen(pDir_fixbkg[G_DispMode],"rb");
	if(p!=NULL)
	{
	
		int readlen = 0;
		readlen = fread(userbkgnd_bmp,4*1366*768,1,p);

		BMPINFO* info = bmp_getinfo(userbkgnd_bmp);
		if(info->bpp==16)
		{
		for(i = 0;i<(int)(info->height);i++)
		{
			memcpy(&userbkgnd_bmp_data[((int)(info->height)-1-i)*2*(int)(info->width)],info->pixel_array+i*(int)(info->width)*2,(int)(info->width)*2);
			//usleep(1);
		}
		}
		else
		{
			for(i = 0;i<(int)(info->height);i++)
			{
			memcpy(&userbkgnd_bmp_data[((int)(info->height)-1-i)*4*(int)(info->width)],info->pixel_array+i*(int)(info->width)*4,(int)(info->width)*4);
			//usleep(1);
			}
			unsigned short * pu16Temp;
			unsigned char u8R, u8G, u8B;	
			
			pu16Temp = (unsigned short *)userbkgnd_bmp;
			memset(userbkgnd_bmp,0,4*1366*768);
			for ( i = 0; i < (int)(info->width)*(int)(info->height); i++ )
			{
				u8B = userbkgnd_bmp_data[i*4];
				u8G = userbkgnd_bmp_data[i*4+1];
				u8R = userbkgnd_bmp_data[i*4+2];
				
				pu16Temp[i] = ( ( u8R&0xff )>>3 )<<11 | ( (u8G&0xff)>>2 )<<5 | (u8B&0xff )>>3;
			
			}		
			memcpy(userbkgnd_bmp_data,userbkgnd_bmp,4*1366*768);	
		}
	}
	else
	{
		short *puser;
		printf("open fixedbkgnd.bmp failed\n");
		memset(userbkgnd_bmp_data,0,1366*768*4);
		puser = (short *)userbkgnd_bmp_data;
		for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX/2;i++)
		{
			*puser = 0x001F;//BLUE
			puser++;
		}
		for(i=0;i<GUI_LCM_XMAX*GUI_LCM_YMAX/2;i++)
		{
			*puser = 0xF800;//RED
			puser++;
		}
		
	}
	v4a_drawrect(&bkgnd_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
	v4a_fillvram(V4A_BKGNDBASE0,GUI_LCM_XMAX*GUI_LCM_YMAX/2,(int *)userbkgnd_bmp_data);
	
	return 0;
}

int main()
{	
	int i;
	int num = 0;
	int cc;
	int INSERT_FLAG = 0;
	int mute_flag=0;
	
	unlink(FIFO);//如果管道存在，先删除    
	if (mkfifo( FIFO , 0x600)==-1) //创建有名管道
	{  
		perror("mkfifo" );	 
	} 
	 
	if ((fifo_fd=open(FIFO,O_RDWR))==-1)  
	{  
		perror("open" );  
		return 1;   
	}
	 
	pthread_t tid_get_lift, tid_get_lift1, tid_get_lift2;
	
	pthread_create(&tid_get_lift,NULL,get_lift_pthread0,NULL); 
	//pthread_create(&tid_get_lift1,NULL,get_lift_pthread1,NULL); 
	pthread_create(&tid_get_lift2,NULL,Appease_Music_pthread,NULL); 
	
	srand((unsigned)time(NULL));
	
	memset(lift_info_bak,0,6);
	lift_info_bak[5] = '\0';

	G_DispMode = GetSysCfg();
	G_REG_VIDEO.x_len = G_REG_VIDEO.x_len/2*2;
	printf("\n\n%d\n\n",G_DispMode);
	if((G_DispMode == dmode_90C) || (G_DispMode == dmode_270C))
	{
		i = GUI_LCM_XMAX;
		GUI_LCM_XMAX = GUI_LCM_YMAX;
		GUI_LCM_YMAX = i;
	}
	
	if(G_DispMode == dmode_error)
	{
		G_DispMode = dmode_0C;
		LCD_CLK = 1;
		GUI_LCM_XMAX = 800;
		GUI_LCM_YMAX = 600;
	}
	
	InitialGUI();
	InitialFileRAMSruct();
	v4a_initial();
	v4a_initscreen(GUI_LCM_XMAX,GUI_LCM_YMAX);
	
	DisplayRegion[9].x_len = DisplayRegion[9].x_len/2*2;
	DisplayRegion[9].y_len = DisplayRegion[9].y_len/2*2;
	//if( (DisplayRegion[9].x_len<=1000)&&(DisplayRegion[9].y_len<=600) )
		//v4a_setvideoarea(gui_rect(DisplayRegion[9].s_x,DisplayRegion[9].s_y,DisplayRegion[9].x_len,DisplayRegion[9].y_len));
	//else
	InitLCM(DisplayRegion[9].x_len,DisplayRegion[9].y_len);
	v4a_drawrect(&bkgnd_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
	
	Display_Print_Init();
	Config_Init();
	Volume_Config();
	Display_Print("Software Version: STEP_EIOT_V2.9");
	
	v4a_enablescreen(1);
	v4a_displaybkgnd(1);
	sleep(3);
	Display_Fixedbkgnd();
	
	CopyAllToRAM();
	enable_backlight();
	floormap_init();
	v4a_drawrect(&overlay_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
START:		
		
	v4a_displaybkgnd(1);
	v4a_displayvideo(1);
	v4a_displayoverlay(1);
	
	get_media_list();
	get_program_list();
	get_weather_info();
	display_user_picture();
	send_cmd();
	while(1){
		int y,z;
		
		if( (lift_info_bak[0] == dir_num)&&(lift_info_bak[1] == state_num)&&(lift_info_bak[2] == flr_n.flr1)&&(lift_info_bak[3] == flr_n.flr2)&&(lift_info_bak[4] == flr_n.flr3) )
				mute_flag++;
			else
			{
				lift_info_bak[0] = dir_num;
				lift_info_bak[1] = state_num;
				lift_info_bak[2] = flr_n.flr1;
				lift_info_bak[3] = flr_n.flr2;
				lift_info_bak[4] = flr_n.flr3;				
				send_mute(0);					
				mute_flag = 0;
			}
			if(mute_flag>=24)
			{				
				send_mute(1);
				mute_flag = 0;
			}
		
		//if(Emergency_Flag)
		//{
			//v4a_displayflex(0);
		//}
		//else
		//{
			display_char_func();
			//v4a_displayflex(1);
		//}
		
		for(y=0;y<10;y++)
		{
			//display_user_picture();
			display_picture_in_video(state_num);			
			//for(z=0;z<15;z++)
			//{
				if(date_config_flag)
					goto DATECFG;
					
				if(Logo_Flag)
				{
					DisplayLogo();
					Logo_Flag = 0;					
				}
				
				if(Weather_Flag)
				{
					DisplayWeather();
					Weather_Flag = 0;
				}
				
				//if(Notice_Flag)
				//{
					//display_char_func();
					//Notice_Flag = 0;
				//}						
				
				DisplayTime();
			
				DisplayDay();
				
				DisplayWeek();
				usleep(400000);
								
				DisplayDir(dir_num);
				DisplayFloor(flr_n);
				DisplayState(state_num);
				
				if( cc=usbdisk_is_insert() )
				{
					if(INSERT_FLAG == 0)
					{
					sleep(8);
					system("mount -t ext3 /dev/sdb1 /mnt/sdcard");
					system("mount -t vfat /dev/sdb1 /mnt/sdcard");
					system("mount -t ext3 /dev/sdc1 /mnt/sdcard");
					system("mount -t vfat /dev/sdc1 /mnt/sdcard");
					
					//system("killall -2 mpl_all");
					}
					INSERT_FLAG =1;
					if( access("/mnt/sdcard/LIFT", 0) == 0 )
					{	
						goto UPDATE;		
					}
					else
					{
						continue;	
					}	
										
				}
				else
				{	
					if(INSERT_FLAG == 1)
						system("reboot");
					INSERT_FLAG = 0;
				}	
			//}
			
		}
	}
	
	DATECFG:
		Dt_last_weekday = -1;
		v4a_drawrect(&overlay_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
		v4a_drawrect(&bkgnd_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
		v4a_displayvideo(0);
		v4a_displayflex(0);
		Config_Init();
		while(1)
		{
			Display_Print_Init();
			Display_Date_Time("<Configuration Menu>");
			Display_Date_Time("Software Version: STEP_EIOT_V2.9");
			Date_Time_Config();
			
			//Display_Date_Time("VOL=[20]");
			//Display_Date_Time("LANGUAGE=CN");
			//Display_Date_Time("BKGND_TIME=[5]");
			//Display_Date_Time("LIGHT_AUTO_OFF=[15]");
			
			Volume_Config();
			Language_Config();
			Bkgnd_Time_Config();
			Light_Off_Time_Config();
			Sleep_Brightness_Config();
			
			usleep(300000);
			
			if(date_config_flag==0)
			{
				v4a_displaybkgnd(0);
				Display_Fixedbkgnd();
				
				Notice_Flag = 1;
				Weather_Flag = 1;
				Logo_Flag =1;
				
				goto START;
			}		
		}
	

	UPDATE:
	send_pause();
	v4a_drawrect(&overlay_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
	v4a_drawrect(&bkgnd_fbdisplay,gui_rect(0,0,GUI_LCM_XMAX,GUI_LCM_YMAX),ARGB(0,0,0,0));
	v4a_displayvideo(0);
	v4a_displayflex(0);
	Display_Print_Init();
	Display_Print("Find LIFT in USB Disk");
	ProcessSDStatus();
	
	while( cc=usbdisk_is_insert() );
	
	sleep(1);
	system("reboot");

	pthread_join(tid_get_lift,NULL);
	//pthread_join(tid_get_lift1,NULL);
	pthread_join(tid_get_lift2,NULL);

	return 0;
}	

